﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shumei.Common;
using System.Threading.Tasks;
using Shumei.EFCore.SimpleService;
using System.Drawing;
using source.ViewModel;
using System.Web.Security;
namespace source.Controllers
{
    public class AccountController : Controller
    {
        private UserService userService;
        public AccountController() { userService = new UserService(); }

        public static bool isLogin(HttpContextBase context)
        {
            var user = context.Session["User"];
            if (user != null)
            {
                return true;
            }

            return false;
        }
        public static UserInfoViewModel GetUserInfo(HttpContextBase context)
        {
            var user = context.Session["User"];
            if (user != null)
            {
                return (UserInfoViewModel)user;
            }
            return null;
        }

        [HttpGet]
        public ActionResult Login()
        {
            //判断是否已经登录
            return View();
        }
       
        [HttpPost]
        public async Task<ActionResult> Login(LoginViewModel model)
        {
            if(ModelState.IsValid)
            {
                var res = await userService.FindAsync(model.Account);
                if (res == null)
                    ModelState.AddModelError("Account", "用户名或者密码错误");
                if (res != null)
                {
                    var hashPwd2 = Security.Sha256(model.Password);
                    if (Security.Sha256(model.Password).Equals(res.password))
                    {
                        //使用Session 记录登录信息
                        this.HttpContext.Session["User"] = res;
                        var url = new UrlHelper(this.HttpContext.Request.RequestContext);
                        return new RedirectResult(url.Action("Index", "Home"));
                    }
                }
            }

            //登录失败
            return View();
        }
        [HttpPost]
        public bool Logout(string account)
        {
            if(this.HttpContext.Session["User"] != null)
            {
                this.HttpContext.Session["User"] = null;
            }
            return true;
        }

        /// <summary>
        /// 验证码的图片
        /// </summary>
        /// <returns></returns>
        public ActionResult VerificationCode()
        {
            string verificationCode = Security.CreateVerificationText(6);
            Bitmap _img = Picture.CreateVerificationImage(verificationCode, 160, 30);
            _img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            TempData["VerificationCode"] = verificationCode.ToUpper();
            return null;
        }

        ///=================验证码图片

        ////
        // <div class="form-group">
        //    @Html.LabelFor(model => model.VerificationCode, new { @class = "control-label col-md-2" })
        //    <div class="col-md-10">
        //        @Html.EditorFor(model => model.VerificationCode)
        //        <img id="verificationcode" title="点击刷新" src="@Url.Action("VerificationCode")" style="cursor:pointer"  />
        //        @Html.ValidationMessageFor(model => model.VerificationCode)
        //    </div>
        //</div>
        //<script type="text/javascript">
        //    $("#verificationcode").click(function () {
        //        $("#verificationcode").attr("src", "@Url.Action("VerificationCode")?" + new Date());
        //    })
        //</script>
    }
}